package com.xxgc.demo.controller.interceptor;

import com.xxgc.demo.controller.annotations.CheckIdCardDate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:SJY
 * @Date :2023/9/12 - 09 - 12 - 11:05
 * 身份证日期拦截校验（拦截器）
 */
public class CheckIdCardDateInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断handler是否是Method对象
        if (handler instanceof HandlerMethod) {
            HandlerMethod method = (HandlerMethod) handler;
            // 获取方法上的所有注解，检查是否存在CheckIdCardDate注解
            if (method.getMethodAnnotation(CheckIdCardDate.class) != null) {
                //☆☆☆☆☆☆☆☆☆ request的流只能够读一次
                ServletInputStream inputStream = request.getInputStream();
                //解决方案 重写request的getInputStream()方法

                return true;
            }
        }
        return true;
    }
}
